﻿using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ST.Buz.Base.Entity;
using ST.Buz.Base.ViewModel;
using ST.Buz.Service;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

namespace ST.Buz.Api.Controllers
{
	[Route("sys/common")]
	public class SysController : BaseJsonController
	{
		public SysController(ILogger<SysController> _logger)
			: base(_logger)
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		[HttpGet("index")]
		[ValidateSession]
		public IActionResult Index()
		{
			try
			{
				UserSession sessionCurrent = GetSessionFromRequest();
				ViewBag.ServiceCode = "4EA7FD46F1";
				return View();
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, ex.StatusCode);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "打开页面出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		[HttpGet("config")]
		[ValidateSession]
		public IActionResult Config()
		{
			try
			{
				UserSession sessionCurrent = GetSessionFromRequest();
				ViewBag.CurrentMenu = "C065EF8575";//子菜单，需要修改与AdminMenuViewComponent一致
				return View();
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, ex.StatusCode);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "打开页面出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		[HttpGet("icons")]
		public IActionResult Icons(string callback)
		{
			try
			{
				ViewData["callback"] = string.IsNullOrEmpty(callback) ? "popIconCallback" : callback;
				return View();
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, ex.StatusCode);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "打开页面出错", (int)HttpStatusCode.InternalServerError);
			}
		}

		public IActionResult Browser()
		{
			return View();
		}

		[HttpGet("report")]
		public JsonResult ReportError()
		{
			try
			{
				var feature = HttpContext.Features.Get<IExceptionHandlerFeature>();
				var error = feature?.Error;
				logger.LogError(error, error.Message);
				return new JsonResultBase(false, error.Message, (int)HttpStatusCode.InternalServerError);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, ex.StatusCode);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.InternalServerError);
			}
			catch (ArgumentException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.InternalServerError);
			}
		}

		[HttpGet("dict")]
		public JsonResult GetDict(string code)
		{
			switch (code)
			{
				//case "001":
				//	{
				//		JsonResultBase ret = new JsonResultBase(GetEnums(typeof(BuzStateEnum)));
				//		return ret;
				//	}
				//case "002":
				//	{
				//		JsonResultBase ret = new JsonResultBase(GetEnums(typeof(QJFSEnum)));
				//		return ret;
				//	}
				default:
					{
						return new JsonResultBase(false, "参数代码有误", (int)HttpStatusCode.BadRequest);
					}
			}
		}

		[HttpGet("upload")]
		public ActionResult Upload(string callback, string infoid, string tbl)
		{
			ViewData["callback"] = callback;
			ViewData["infoid"] = infoid;
			ViewData["tbl"] = tbl;
			return View();
		}

#if DEBUG
		#region 测试方法
		[HttpGet("test")]
		public IActionResult Test()
		{
			try
			{
				return new JsonResultBase("test");
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "打开页面出错", (int)HttpStatusCode.InternalServerError);
			}
		}
		#endregion
#endif
	}
}